﻿var gQueuesLoading = false;
var gEnabled = false;

//called on setting load (Note different context to main - no varibles are shared!)
function onLoadSettingsScreen() {
    System.Gadget.onSettingsClosing = onSettingsClosing;
    GetSettings();
    pnlError.style.display = "none";
    CheckReq();
    LoadAzureQueues();

    //set fields
    txtAccountName.value = AccountName;
    txtSharedKey.value = SharedKey;
    txtYellowAlert.value = YellowAlert;
    txtRedAlert.value = RedAlert;
    txtPollTime.value = PollTime;
}

//called when settings are closed - save if action commit
function onSettingsClosing(event) {
    if (event.closeAction == event.Action.commit) {
        SetSettings();
    }
    event.cancel = false;
}

//parse data into vars
function ParseSettings() {
    if (txtAccountName.value.length > 0) AccountName = txtAccountName.value;
    if (txtSharedKey.value.length > 0) SharedKey = txtSharedKey.value;
    if (parseInt(txtYellowAlert.value) > 0) YellowAlert = parseInt(txtYellowAlert.value);
    if (parseInt(txtRedAlert.value) > 0) RedAlert = parseInt(txtRedAlert.value);
    if (parseInt(txtPollTime.value) > 0) PollTime = parseInt(txtPollTime.value);
    if (!selQueues.disabled) {
        try {
            QueueName = selQueues.options[selQueues.selectedIndex].value;
        } catch (e) {
            QueueName = "";
        }
    }
    CheckReq();
}

function CheckReq() {
    //are we valid enough to enable the query?
    if (AccountName.length > 0 && SharedKey.length > 0) {
        gEnabled = true;
    }
}

//save current settings
function SetSettings() {
    ParseSettings();
    System.Gadget.Settings.write("SavedSetting", 1);
    System.Gadget.Settings.write("AccountName", AccountName);
    System.Gadget.Settings.write("SharedKey", SharedKey);
    System.Gadget.Settings.write("YellowAlert", YellowAlert);
    System.Gadget.Settings.write("RedAlert", RedAlert);
    System.Gadget.Settings.write("PollTime", PollTime);
    System.Gadget.Settings.write("QueueName", QueueName);
}

function LoadAzureQueues() {
    if (gEnabled && !gQueuesLoading) {
        gQueuesLoading = true;
        selQueues.disabled = true;
        //clear out queue dropdown
        for (var x = 0; x < selQueues.length; x++) {
            selQueues.remove(x)
        }
        LoadingAnimation.style.display = "block";
        pnlError.style.display = "none";
        try {
            var xmlhttp = new XMLHttpRequest();
            var command = "comp=list";

            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200 || window.location.href.indexOf("http") == -1) {
                        //parse this
                        var xmlstr = xmlhttp.responseText;
                        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                        xmlDoc.async = "false";
                        xmlDoc.loadXML(xmlstr);
                        var queues = xmlDoc.getElementsByTagName("QueueName");
                        if (queues.length > 0) {
                            var foundindex = 0;
                            for (var x = 0; x < queues.length; x++) {
                                var nodeValue = queues[x].childNodes[0].nodeValue;
                                selQueues.add(new Option(nodeValue, nodeValue));
                                if (nodeValue == QueueName) {
                                    foundindex = x;
                                }
                            }
                            selQueues.disabled = false;
                            //set the QueueName Option as selected if valid
                            selQueues.selectedIndex = foundindex;
                        } else {
                            pnlError.style.display = "block";
                        }
                        LoadingAnimation.style.display = "none";
                        gQueuesLoading = false;
                    }
                    else {
                        pnlError.style.display = "block";
                        LoadingAnimation.style.display = "none";
                        gQueuesLoading = false;
                    }
                }
            }
            AzureHelper.AddHeaders(xmlhttp, AccountName, Storage, command, SharedKey, "");
            xmlhttp.send(null);
        } catch (e) {
            pnlError.style.display = "block";
            LoadingAnimation.style.display = "none";
            gQueuesLoading = false;
        }
    }
}

function onVerifyClick() {
    ParseSettings();
    LoadAzureQueues();
}